package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sl.au.entity.ReplaceDevicePrice;
import com.sl.au.entity.SinglePump;
import com.sl.au.repository.ReplaceDevicePriceRepository;
import com.sl.au.service.ReplaceDevicePriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Device;
import com.sl.au.repository.DeviceRepository;
import com.sl.au.service.DeviceService;
import com.sl.au.service.ExcelService;
import com.sl.utils.CRM;

@Service("replaceDeviceService")
@Transactional
public class ReplaceDevicePriceServiceImpl extends BaseServiceImpl implements ReplaceDevicePriceService {

    @Autowired
    ReplaceDevicePriceRepository replaceDevicePriceRepository;

    @Autowired
    ExcelService excelService;



    
    @Override
    public List<ReplaceDevicePrice> getReplaceDevices(int start, int length, String key, String superior,String dataFilter, String orderName, String dir) {
    	Page<ReplaceDevicePrice> pages = null;
    	Pageable pageable=null;
    	if(dataFilter.equals("所有")){
    		pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "reDeviceName","superior");
    		pages = replaceDevicePriceRepository.getReplace(key, pageable);
        }else{
        	pageable = new PageRequest(start/length, length, Sort.Direction.DESC, "createTime");
        	pages=replaceDevicePriceRepository.getReplace(superior, key, pageable);
        }
        return pages.getContent();
    }



    public ReplaceDevicePriceRepository getRepository() {
        return this.replaceDevicePriceRepository;
    }

    @SuppressWarnings("unchecked")
    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public long importExcel(String fileName, String superior) {
        long count=0;
        if (!StringUtils.hasText(fileName))
            return count;
        Map<String, Object> collections = new HashMap<>();
        //excelService.setStorageLocation(CRM.getStorageLocation());
        excelService.importExcel("priceofreplacedevice", collections,fileName);
        if (collections.size() < 1 || collections.get("items") == null)
            return count;
        List<ReplaceDevicePrice> rows = (List<ReplaceDevicePrice>) collections.get("items");
        List<ReplaceDevicePrice> prices=this.getChecked(rows, superior);
        if (null!=prices && prices.size() > 0) {
            this.replaceDevicePriceRepository.save(prices);
            count=rows.size();
        }
        return count;
    }



	@Override
	public List<ReplaceDevicePrice> getChecked(List<ReplaceDevicePrice> rows, String superior) {
		List<ReplaceDevicePrice> prices=new ArrayList<ReplaceDevicePrice>();
		for(ReplaceDevicePrice r:rows){
			String name=r.getReDeviceName();
			String type=r.getReType();
			String material=r.getReMaterial();
			ReplaceDevicePrice p=this.getRepository().findByNameAndSuperior(name,type,material, superior);
			if(p!=null){
				p.setPrice(r.getPrice());
				prices.add(p);	
			}	
		}
		return prices;
	}

}
